// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using JetBrains.Annotations;
using Oracle.ManagedDataAccess.Client;

namespace SnapObjects.Data.Oracle
{
    public class OracleByteArrayTypeMapping : OracleTypeMapping
    {
        private readonly int _maxSpecificSize;

        /// <summary>
        ///     Initializes a new instance of the <see cref="OracleByteArrayTypeMapping" /> class.
        /// </summary>
        /// <param name="dbTypeName"> The name of the database type. </param>
        /// <param name="dbType"> The <see cref="System.Data.DbType" /> to be used. </param>
        /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
        public OracleByteArrayTypeMapping(
            [NotNull] string dbTypeName,
            [CanBeNull] DbType? dbType = System.Data.DbType.Binary,
            int? size = null)
            : base(dbTypeName, typeof(byte[]), dbType, size: size)
        {
            _maxSpecificSize = CalculateSize(size);
        }

        private static int CalculateSize(int? size)
        {
            return size.HasValue && size < Int32.MaxValue ? size.Value : Int32.MaxValue;
        }

        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override TypeMapping Clone(string dbTypeName, int? size)
        {
            return new OracleByteArrayTypeMapping(
                           dbTypeName,
                           this.DbType,
                           size);
        }

        protected override void ConfigureParameter([NotNull] DbParameter parameter, IAdoDbParameter adoDbParameter)
        {
            if (parameter is OracleParameter oracleParameter)
            {
                this.ConfigureDbType(oracleParameter);
            }

            parameter.Size = _maxSpecificSize;
        }

        private void ConfigureDbType(OracleParameter parameter)
        {
            if (!String.IsNullOrWhiteSpace(this.DbTypeName))
            {
                if (this.DbTypeName.Equals("bfile", StringComparison.OrdinalIgnoreCase))
                {
                    parameter.OracleDbType = OracleDbType.BFile;
                }
                else if (this.DbTypeName.Equals("blob", StringComparison.OrdinalIgnoreCase))
                {
                    parameter.OracleDbType = OracleDbType.Blob;
                }
            }
        }

        /// <summary>
        ///     Generates the SQL representation of a literal value.
        /// </summary>
        /// <param name="value">The literal value.</param>
        /// <returns>
        ///     The generated string.
        /// </returns>
        protected override string GenerateNonNullSqlLiteral(object value)
        {
            var builder = new StringBuilder();
            builder.Append("0x");

            foreach (var @byte in (byte[])value)
            {
                builder.Append(@byte.ToString("X2", CultureInfo.InvariantCulture));
            }

            return builder.ToString();
        }
    }
}
